<template>
  <a-spin tip="Loading..." :spinning="loading">
    <div class="container">
      <div class="bg-lf"></div>
      <div class="form-rf">
        <span class="title">{{ appName }}</span>
        <span class="communit">仙霞新村街道</span>
        <span class="welcome">欢迎使用</span>
        <a-form-model
          :model="form"
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
        >
          <a-form-model-item prop="loginName">
            <a-input
              size="large"
              v-model.trim="form.loginName"
              placeholder="请输入手机号"
              :maxLength="11"
              autocomplete="loginName"
            >
              <a-icon slot="prefix" type="user" />
            </a-input>
          </a-form-model-item>
          <a-form-model-item prop="password">
            <a-input
              size="large"
              v-model.trim="form.password"
              placeholder="请输入密码"
              :maxLength="20"
              type="password"
              autocomplete="password"
            >
              <a-icon slot="prefix" type="lock" />
            </a-input>
          </a-form-model-item>
          <!-- <div class="linkPhone">
            忘记密码请联系管理员 | 联系电话：{{ linkPhone }}
          </div> -->
          <a-form-model-item>
            <a-button
              class="loginbtn"
              type="primary"
              size="large"
              @click="handleLogin()"
              >登录</a-button
            >
          </a-form-model-item>
        </a-form-model>
      </div>
    </div>
  </a-spin>
</template>
<script>
import md5 from "js-md5";
import { RE_PHONE } from "@/utils";
import loginApi from "@/api/login";
import { mapActions } from "vuex";
import { initialRouter } from "@/router";

export default {
  data() {
    return {
      appName: process.env.VUE_APP_NAME,
      // linkPhone: process.env.VUE_APP_PHONE,
      labelCol: { span: 24 },
      wrapperCol: { span: 24 },
      loading: false,
      form: {
        loginName: "",
        password: ""
      }
    };
  },
  methods: {
    ...mapActions({
      saveUserInfo: "user/saveUserInfo",
      initalAuth: "user/initalAuth"
    }),
    handleLogin() {
      if (!this.form.loginName) {
        this.$message.warning("请输入手机号");
      } else if (!RE_PHONE.test(this.form.loginName)) {
        this.$message.warning("请输入正确的11位手机号");
      } else if (!this.form.password) {
        this.$message.warning("请输入密码");
      } else {
        this.login(this.form);
      }
    },
    login(form) {
      this.loading = true;
      let data = {
        loginName: form.loginName,
        pwd: md5(form.password)
      };
      loginApi
        .login(data)
        .then((res) => {
          this.loading = false;
          if (res.retcode === 0) {
            this.saveStore(res.data);
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch((err) => {
          this.loading = false;
          this.$message.error(err.msg);
        });
    },
    saveStore(data) {
      if (!data) return;
      this.saveUserInfo({
        token: data.jwttoken,
        member: data.user
      })
        .then(() => {
          this.$message.success("您好！欢迎使用社区后台管理系统", 1);
          initialRouter(data.user.roleCode);
          this.initalAuth(data.user.roleCode);
          setTimeout(() => {
            this.$router.replace("/owner/index");
          }, 600);
        })
        .catch((err) => {
          console.log(err);
        });
    }
  }
};
</script>
<style scoped lang="less">
.container {
  height: 100%;
  display: flex;
  flex-flow: row nowrap;
  align-items: center;

  .bg-lf {
    width: 53%;
    height: 100%;
    background: url("~@/assets/images/bg@2x.png") center center no-repeat;
    background-size: 100% 100%;
  }

  .form-rf {
    width: 47%;
    height: 100%;
    display: flex;
    flex-flow: column nowrap;
    justify-content: center;
    align-items: center;
    .title {
      height: 40px;
      font-size: 34px;
      font-family: FZLTZHK--GBK1-0, FZLTZHK--GBK1;
      font-weight: normal;
      color: #000000;
      line-height: 40px;
      letter-spacing: 1px;
    }
    .communit {
      height: 33px;
      font-size: 24px;
      font-family: FZLTZHK--GBK1-0, FZLTZHK--GBK1;
      font-weight: 400;
      line-height: 33px;
      margin-top: 20px;
    }
    .welcome {
      height: 33px;
      font-size: 24px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: rgba(0, 0, 0, 0.45);
      line-height: 33px;
      margin: 20px 0;
    }
    .ant-form {
      width: 54%;
      padding: 0 60px;

      // .linkPhone {
      //   width: 100%;
      //   font-family: PingFangSC-Regular, PingFang SC;
      //   color: #bfbfbf;
      //   padding-bottom: 24px;
      //   text-align: center;
      // }

      .loginbtn {
        width: 100%;
        height: 40px;
      }
    }
  }
}
</style>
